home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / yeah09.zip / yea.h < prev    next >
C/C++ Source or Header  |  1996-05-25  |  17KB  |  618 lines

  1. #ifndef YEA_H
  2. #define YEA_H
  3.  
  4. //------------------------------------------------------------
  5. //
  6. // Name:     yea.h
  7. // Version:  0.9
  8. // Author:   Björn Fahller.
  9. //
  10. // Copyright (C) Björn Fahller, 1996.
  11. //
  12. // Purpose:  All necessary declarations for YEAH, both the base
  13. //           functionality (class EA and TEA<T>) and the provided
  14. //           implementations of StringEA, SequenceEA and
  15. //           TSequenceEA<T>
  16. //
  17. // History:
  18. //          Ver.  Date         What
  19. //          0.9   1996-05-26   First official release.
  20. //
  21. //------------------------------------------------------------
  22.  
  23.  
  24. class fstreambase;
  25. struct _EAOP2;
  26.  
  27. #include <IString.hpp>
  28. #include <IMap.h>
  29. #include <IKSSet.h>
  30. #include <ISeq.h>
  31. #include <IPtr.h>
  32.  
  33. #include <IExcept.hpp>
  34. #include <strstrea.h>
  35.  
  36. IEXCLASSDECLARE(EAError, IException);
  37. IEXCLASSDECLARE(EAReadError, EAError);
  38. IEXCLASSDECLARE(EAWriteError, EAError);
  39. IEXCLASSDECLARE(EATypeMismatchError, EAError);
  40. IEXCLASSDECLARE(EATSeqeuceEAInstError,EATypeMismatchError);
  41.  
  42. class EA
  43. {
  44. public:
  45.   typedef unsigned short Identifier;
  46.   struct CreatorIdPair;
  47.   typedef IMap<CreatorIdPair, Identifier> CreatorMap;
  48.   typedef EA* (*Creator)(istrstream&, CreatorMap*);
  49.   struct CreatorIdPair {
  50.     Creator c;
  51.     Identifier id;
  52.     IMngPointer<CreatorMap> pSubMap;
  53.  
  54.     CreatorIdPair(const Creator& cc, Identifier i) : c(cc), id(i) {};
  55.     IBoolean operator==(const CreatorIdPair& i) const { return id == i.id; };
  56.     IBoolean operator<(const CreatorIdPair& i) const { return id < i.id; };
  57.   };
  58.   static CreatorMap defaultCreatorMap;
  59.  
  60.   typedef enum { optional = 0, mandatory = 0x80 } Flagset;
  61.   struct Name {
  62.     IString name;
  63.     Flagset flags;
  64.     Name(const IString& n, Flagset f) : name(n), flags(f) {};
  65.     IBoolean operator==(const Name& n) const { return name == n.name; };
  66.     IBoolean operator<(const Name& n) const { return name < n.name; };
  67.   };
  68.   typedef IKeySortedSet<Name, IString> NameSet;
  69.  
  70.   typedef enum {
  71.     ReadError,
  72.     WriteError,
  73.     NoSuchEAError,
  74.     TypeMismatchError
  75.   } Error;
  76.  
  77.   typedef void (*ErrorHandler)(Error, unsigned long);
  78.   static ErrorHandler errorHandler;
  79.  
  80.   static NameSet namesIn(const IString& file);
  81.   static NameSet namesIn(fstreambase& file);
  82.  
  83.   virtual ~EA(void);
  84.  
  85.   Identifier attributeId(void) const { return id; };
  86.   Flagset getFlags(void) const { return (Flagset)flags;};
  87.   void setFlags(Flagset f) { flags = (char)f; };
  88.  
  89.   static EA* newFrom(const IString& file,
  90.                      const IString& name,
  91.                      const CreatorMap& = defaultCreatorMap);
  92.   static EA* newFrom(fstreambase& file,
  93.                      const IString& name,
  94.                      const CreatorMap& = defaultCreatorMap);
  95.  
  96.   static void remove(const IString& file, const IString& name);
  97.   static void remove(fstreambase& file, const IString& name);
  98.  
  99.   void getFrom(const IString& file, const IString& name);
  100.   void getFrom(fstreambase& file, const IString& name);
  101.  
  102.   void storeTo(const IString& file, const IString& name);
  103.   void storeTo(fstreambase& file, const IString& name);
  104.  
  105.   virtual EA* clone(void) const = 0;
  106.   virtual void setCreatorMap(const CreatorMap*) {};
  107. protected:
  108.   EA(Identifier anId);
  109.   EA(const EA&);
  110.   const EA& operator=(const EA&);
  111.  
  112.   static istrstream& read(EA* pea, istrstream& is)
  113.     { return pea->readFrom(is);};
  114.   static ostrstream& write(EA* pea, ostrstream& os)
  115.     { return pea->writeTo(os);};
  116.  
  117.   virtual istrstream& readFrom(istrstream&) = 0;
  118.   virtual ostrstream& writeTo(ostrstream&) = 0;
  119. private:
  120.   void setupFEA(const IString&);
  121.   void feaproc(void);
  122.  
  123.   Identifier id;
  124.   _EAOP2* peaop2;
  125.   char flags;
  126.  
  127.   friend EA* createFrom(_EAOP2*, const EA::CreatorMap&);
  128. };
  129.  
  130. template <class T, EA::Identifier eaid>
  131. class TEA : public EA
  132. {
  133. public:
  134.   enum { id = eaid };
  135.  
  136.   static T* cast(EA*);
  137.   static const T* cast(const EA*);
  138.   virtual ~TEA(void) {};
  139.  
  140.   static void allowDynamic(EA::CreatorMap* pCM = &EA::defaultCreatorMap,
  141.                            const EA::Creator& creator =  createFrom);
  142.   static void disallowDynamic(EA::CreatorMap* pCM = &EA::defaultCreatorMap);
  143. protected:
  144.   TEA(void) : EA(eaid) {};
  145.   TEA(const TEA<T, eaid>&t) : EA(t) {};
  146.   const TEA<T, eaid>& operator=(const TEA<T, eaid>& t)
  147.     { EA::operator=(t); return *this;};
  148. private:
  149.   static EA* createFrom(istrstream&, EA::CreatorMap*);
  150.  
  151.   static NameSet namesIn(const IString& file);
  152.   static NameSet namesIn(fstreambase& file);
  153.   static EA* newFrom(const IString& file,
  154.                      const IString& name,
  155.                      const CreatorMap& = defaultCreatorMap);
  156.   static EA* newFrom(fstreambase& file,
  157.                      const IString& name,
  158.                      const CreatorMap& = defaultCreatorMap);
  159.   static void remove(const IString& file, const IString& name);
  160.   static void remove(fstreambase& file, const IString& name);
  161.   static istrstream& read(EA*,istrstream&);
  162.   static ostrstream& write(EA*,ostrstream&);
  163.   class Creator {};
  164.   class CreatorMap {};
  165.   class CreatorIdPair {};
  166.   class Name {};
  167.   class Error {};
  168.   class ErrorHandler {};
  169.   class Flagset {};
  170.   class Identifier {};
  171.   class NameSet {};
  172. };
  173.  
  174.  
  175. class StringEA : public TEA<StringEA, 0xfffd>,
  176.                  public IString
  177. {
  178. public:
  179.   StringEA(void) : IString() {};
  180.   StringEA(const void* pBuffer, unsigned lenBuffer, char padCharacter = ' ')
  181.     : IString(pBuffer, lenBuffer, padCharacter) {};
  182.   StringEA(const IString& s) : IString(s) {  };
  183.   StringEA(int i) : IString(i) { };
  184.   StringEA(unsigned u) : IString(u) {  };
  185.   StringEA(double d) : IString(d) {  };
  186.   StringEA(char c) : IString(c) {  };
  187.   StringEA(unsigned char uc) : IString(uc) {  };
  188.   StringEA(signed char sc) : IString(sc) {  };
  189.   StringEA(const char* p) : IString(p) {  };
  190.   StringEA(const unsigned char* p) : IString(p) {  };
  191.   StringEA(const signed char* p) : IString(p) {  };
  192.   StringEA(const void* pBuffer1, unsigned lenBuffer1,
  193.            const void* pBuffer2, unsigned lenBuffer2,
  194.            char padCharacter = ' ') : IString(pBuffer1, lenBuffer1,
  195.                                               pBuffer2, lenBuffer2,
  196.                                               padCharacter) {};
  197.   StringEA(const void* pBuffer1, unsigned lenBuffer1,
  198.            const void* pBuffer2, unsigned lenBuffer2,
  199.            const void* pBuffer3, unsigned lenBuffer3,
  200.            char padCharacter = ' ') : IString(pBuffer1, lenBuffer1,
  201.                                               pBuffer2, lenBuffer2,
  202.                                               pBuffer3, lenBuffer3,
  203.                                               padCharacter) {};
  204.  
  205.   StringEA(const StringEA& s) : TEA<StringEA, StringEA::id>(s), IString(s) {};
  206.   virtual ~StringEA(void) {};
  207.  
  208.   const StringEA& operator=(const StringEA& s)
  209.   {
  210.     TEA<StringEA, StringEA::id>::operator=(s);
  211.     IString::operator=(s);
  212.     return *this;
  213.   };
  214.   const StringEA& operator=(const IString& s)
  215.   {
  216.     IString::operator=(s);
  217.     return *this;
  218.   };
  219.  
  220.   StringEA(const IString& filename, const IString& eaname)
  221.     { getFrom(filename, eaname);};
  222.   StringEA(fstreambase& file, const IString& eaname)
  223.     { getFrom(file, eaname); };
  224.  
  225.   virtual StringEA* clone(void) const;
  226.  
  227. protected:
  228.   virtual istrstream& readFrom(istrstream&);
  229.   virtual ostrstream& writeTo(ostrstream&);
  230. private:
  231. };
  232.  
  233. class MTSequenceEA : public TEA< MTSequenceEA, 0xffdf>,
  234.                      public ISequence<EA*>
  235. {
  236. public:
  237.   typedef EA* (*ErrorFunction)(EA::Identifier);
  238.   static ErrorFunction errorFunction;
  239.  
  240.   MTSequenceEA(const EA::CreatorMap* = &EA::defaultCreatorMap);
  241.   MTSequenceEA(const MTSequenceEA& mvea);
  242.   MTSequenceEA(const ISequence<EA*>&,
  243.                const EA::CreatorMap* = &EA::defaultCreatorMap);
  244.   MTSequenceEA(const IString& filename,
  245.                const IString& eaname,
  246.                const EA::CreatorMap* = &EA::defaultCreatorMap);
  247.   MTSequenceEA(fstreambase& file,
  248.                const IString& eaname,
  249.                const EA::CreatorMap* = &EA::defaultCreatorMap);
  250.  
  251.   const MTSequenceEA& operator=(const MTSequ